<template>
  <view class="home-container">
    <!-- 顶部状态栏 -->
    <view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
    
    <!-- 自定义导航栏 -->
    <view class="custom-navbar">
      <view class="navbar-content">
        <view class="navbar-left">
          <image class="avatar" :src="userInfo.avatar || '/static/default-avatar.png'" @click="handleAvatarClick"></image>
          <view class="user-info">
            <text class="username">{{ userInfo.username || '用户' }}</text>
            <text class="welcome">欢迎回来</text>
          </view>
        </view>
        
        <view class="navbar-right">
          <uni-icons type="search" size="24" color="#333" @click="handleSearch"></uni-icons>
          <uni-icons type="notification" size="24" color="#333" @click="handleNotification" style="margin-left: 20rpx;"></uni-icons>
        </view>
      </view>
    </view>

    <!-- 轮播图 -->
    <view class="banner-section">
      <swiper class="banner-swiper" indicator-dots circular autoplay>
        <swiper-item v-for="(banner, index) in bannerList" :key="index">
          <image class="banner-image" :src="banner.image" mode="aspectFill" @click="handleBannerClick(banner)"></image>
        </swiper-item>
      </swiper>
    </view>

    <!-- 功能菜单 -->
    <view class="menu-section">
      <view class="menu-grid">
        <view 
          class="menu-item" 
          v-for="(menu, index) in menuList" 
          :key="index"
          @click="handleMenuClick(menu)"
        >
          <view class="menu-icon" :style="{ background: menu.color }">
            <uni-icons :type="menu.icon" size="28" color="#fff"></uni-icons>
          </view>
          <text class="menu-text">{{ menu.name }}</text>
        </view>
      </view>
    </view>

    <!-- 内容区域 -->
    <view class="content-section">
      <view class="section-header">
        <text class="section-title">推荐内容</text>
        <text class="section-more" @click="handleMore">更多</text>
      </view>
      
      <view class="content-list">
        <view 
          class="content-item" 
          v-for="(item, index) in contentList" 
          :key="index"
          @click="handleContentClick(item)"
        >
          <image class="content-image" :src="item.image" mode="aspectFill"></image>
          <view class="content-info">
            <text class="content-title">{{ item.title }}</text>
            <text class="content-desc">{{ item.desc }}</text>
            <view class="content-meta">
              <text class="content-time">{{ item.time }}</text>
              <text class="content-views">{{ item.views }}次浏览</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部安全区域 -->
    <view class="safe-area-bottom"></view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 响应式数据
const statusBarHeight = ref(0)
const userInfo = ref({})
const bannerList = ref([
  { id: 1, image: '/static/banner1.jpg', url: '' },
  { id: 2, image: '/static/banner2.jpg', url: '' },
  { id: 3, image: '/static/banner3.jpg', url: '' }
])

const menuList = ref([
  { id: 1, name: '扫一扫', icon: 'scan', color: '#07C160', url: '/pages/scan/scan' },
  { id: 2, name: '付款码', icon: 'wallet', color: '#FF6B35', url: '/pages/pay/pay' },
  { id: 3, name: '收藏', icon: 'heart', color: '#FF1744', url: '/pages/favorite/favorite' },
  { id: 4, name: '设置', icon: 'gear', color: '#6C5CE7', url: '/pages/setting/setting' },
  { id: 5, name: '帮助', icon: 'help', color: '#00CEC9', url: '/pages/help/help' },
  { id: 6, name: '反馈', icon: 'chatbubble', color: '#FDCB6E', url: '/pages/feedback/feedback' },
  { id: 7, name: '关于', icon: 'info', color: '#A29BFE', url: '/pages/about/about' },
  { id: 8, name: '更多', icon: 'more', color: '#636E72', url: '/pages/more/more' }
])

const contentList = ref([
  {
    id: 1,
    title: '移动应用开发最佳实践',
    desc: '分享移动端开发的经验和技巧，帮助开发者提升开发效率',
    image: '/static/content1.jpg',
    time: '2小时前',
    views: 1234
  },
  {
    id: 2,
    title: 'Vue3 + UniApp 实战教程',
    desc: '从零开始学习使用Vue3语法开发跨平台移动应用',
    image: '/static/content2.jpg',
    time: '5小时前',
    views: 2345
  },
  {
    id: 3,
    title: 'UI设计规范与组件库',
    desc: '构建统一的设计语言，提升用户体验和开发效率',
    image: '/static/content3.jpg',
    time: '1天前',
    views: 3456
  }
])

// 方法
const handleAvatarClick = () => {
  uni.navigateTo({
    url: '/pages/profile/profile'
  })
}

const handleSearch = () => {
  uni.navigateTo({
    url: '/pages/search/search'
  })
}

const handleNotification = () => {
  uni.navigateTo({
    url: '/pages/notification/notification'
  })
}

const handleBannerClick = (banner) => {
  if (banner.url) {
    uni.navigateTo({
      url: banner.url
    })
  }
}

const handleMenuClick = (menu) => {
  if (menu.url) {
    uni.navigateTo({
      url: menu.url
    })
  } else {
    uni.showToast({
      title: `点击了${menu.name}`,
      icon: 'none'
    })
  }
}

const handleContentClick = (item) => {
  uni.navigateTo({
    url: `/pages/detail/detail?id=${item.id}`
  })
}

const handleMore = () => {
  uni.navigateTo({
    url: '/pages/content-list/content-list'
  })
}

// 生命周期
onMounted(() => {
  // 获取系统信息（如状态栏高度）
  const systemInfo = uni.getSystemInfoSync();
  statusBarHeight.value = systemInfo.statusBarHeight || 0;

  // 获取用户信息
  const storedUserInfo = uni.getStorageSync('userInfo');
  const token = uni.getStorageSync('token'); // 单独获取 token

  if (storedUserInfo) {
    userInfo.value = storedUserInfo;
  }

  // 检查登录状态（同时验证 token 和用户信息）
  if (!token || !storedUserInfo) {
    console.log('未登录或 token 无效，跳转至登录页');
    uni.reLaunch({
      url: '/pages/login/login',
    });
  }
});
</script>

<style scoped>
.home-container {
  background: #f5f5f5;
  min-height: 100vh;
}

.status-bar {
  background: #fff;
}

.custom-navbar {
  background: #fff;
  padding: 20rpx 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.navbar-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.navbar-left {
  display: flex;
  align-items: center;
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  margin-right: 20rpx;
}

.user-info {
  display: flex;
  flex-direction: column;
}

.username {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.welcome {
  font-size: 24rpx;
  color: #999;
  margin-top: 4rpx;
}

.navbar-right {
  display: flex;
  align-items: center;
}

.banner-section {
  margin: 20rpx 30rpx;
}

.banner-swiper {
  height: 300rpx;
  border-radius: 20rpx;
  overflow: hidden;
}

.banner-image {
  width: 100%;
  height: 100%;
}

.menu-section {
  background: #fff;
  margin: 20rpx 30rpx;
  border-radius: 20rpx;
  padding: 40rpx 20rpx;
}

.menu-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 40rpx 20rpx;
}

.menu-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.menu-icon {
  width: 80rpx;
  height: 80rpx;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16rpx;
}

.menu-text {
  font-size: 24rpx;
  color: #333;
}

.content-section {
  background: #fff;
  margin: 20rpx 30rpx;
  border-radius: 20rpx;
  padding: 30rpx;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 30rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.section-more {
  font-size: 28rpx;
  color: #666;
}

.content-list {
  display: flex;
  flex-direction: column;
  gap: 30rpx;
}

.content-item {
  display: flex;
  gap: 20rpx;
}

.content-image {
  width: 160rpx;
  height: 120rpx;
  border-radius: 12rpx;
  flex-shrink: 0;
}

.content-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.content-title {
  font-size: 30rpx;
  font-weight: bold;
  color: #333;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.content-desc {
  font-size: 26rpx;
  color: #666;
  line-height: 1.4;
  margin: 8rpx 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

.content-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.content-time,
.content-views {
  font-size: 22rpx;
  color: #999;
}

.safe-area-bottom {
  height: env(safe-area-inset-bottom);
}
</style>
